package light.head.dao.oa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.AuditStatus;
import light.head.model.LeaveManage;

@Service
public class LeaveDAO extends BaseDAO<LeaveManage, Integer> {
	
	
	private final static String sql_findAuditStatusBusiness = " FROM AuditStatus a WHERE a.mc = ? AND a.rc = ? AND a.business = ?";
	
	/***
	 * 我的请假申请
	 * @param cu 当前用户编号
	 * @param map
	 * @param pb
	 * @return
	 */
	public RESULT findByMap_1(Integer cu,List<Integer> ocs,  Map<String, String> map, PageBean pb){
		StringBuffer hql = new StringBuffer("FROM LeaveManage l WHERE l.cu = ? AND l.oc IN (");

		List<Object> list = new ArrayList<Object>();	
		list.add(cu);
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		String cuname = map.get(Parm.CUNAME);
		String username = map.get(Parm.USERNAME);
		String start = map.get(Parm.ST);
		String agree = map.get(Parm.AGREE);
		String reason = map.get(Parm.REASON);
		String oc = map.get(Parm.OC);
		String deleted = map.get(Parm.DELETED);
		if (null != map) {
			if(isNotNull(cuname)) {
				hql.append(" AND l.cu in (SELECT u.code FROM UserInfo u WHERE u.realName like ?)");
				list.add("%" + cuname + "%");
			}if(isNotNull(username)) {
				hql.append(" AND l.uc in (SELECT u.code FROM UserInfo u WHERE u.realName like ?)");
				list.add("%" + username + "%");
			}if(isNotNull(start)) {
				hql.append(" AND l.st >= ?");
				list.add(DateUtils.getDateByStr(start, "yyyy-MM-dd"));
			}if(isNotNull(agree)) {
				hql.append(" AND l.agree like ?");
				list.add("%" + agree + "%");
			}if(isNotNull(reason)) {
				hql.append(" AND l.reason like ?");
				list.add("%" + reason + "%");
			}if(isNotNull(oc)) {
				hql.append(" AND l.oc = ?");
				list.add(Integer.parseInt(oc));
			}if(isNotNull(deleted)) {
				hql.append(" AND l.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
			
		}
		hql.append(" ORDER BY l.code DESC");
		
		if (findToPageBean(hql.toString(), pb, list) == RESULT.SUCCESS)
			return RESULT.SUCCESS;
		
		return RESULT.ERROR;
	}
	/***
	 * 请假记录
	 * @param ocs 机构编号集合
	 * @param rc 当前角色编号
	 * @param map
	 * @param pb
	 * @return
	 */
	public RESULT findByMap1(List<Integer> ocs, Integer rc, Map<String, String> map, PageBean pb){
		
		StringBuffer temp = new StringBuffer(" FROM LeaveManage l WHERE l.deleted = 0 AND l.code IN");
		List<Object> list = new ArrayList<Object>();	
		temp.append(" (SELECT s.mc FROM AuditStatus s WHERE s.beforeStatus = 1  AND (s.business = 2 OR s.business = 6) AND s.rc = ? )");
		list.add(rc);
		temp.append("AND l.oc IN (");
		for (Integer i : ocs){
			temp.append("?,");
			list.add(i);
		}
		
		temp.deleteCharAt(temp.length() - 1).append(")");
		
		if (null != map) {
			String cuname = map.get(Parm.CUNAME);
			String username = map.get(Parm.USERNAME);
			String start = map.get(Parm.ST);
			String end = map.get(Parm.END);
			String reason = map.get(Parm.REASON);
			
			if(isNotNull(cuname)) {
				temp.append(" AND l.cu in (SELECT u.code FROM UserInfo u WHERE u.realName like ?)");
				list.add("%" + cuname + "%");
			}
			if(isNotNull(username)) {
				temp.append(" AND l.uc in (SELECT u.code FROM UserInfo u WHERE u.realName like ?)");
				list.add("%" + username + "%");
			}
			if(isNotNull(start)) {
				temp.append(" AND l.st > ?");
				list.add(DateUtils.getDateByStr(start, null));
			}
			if(isNotNull(end)) {
				temp.append(" AND l.end < ?");
				list.add(DateUtils.getDateByStr(end, null));
			}
			if(isNotNull(reason)) {
				temp.append(" AND l.reason like ?");
				list.add("%" + reason + "%");
			} 
			
		}
		temp.append(" ORDER BY l.code DESC");
		
		if (findToPageBean(temp.toString(), pb, list) == RESULT.SUCCESS)
			return RESULT.SUCCESS;
		
		return RESULT.ERROR;
	}
	
	/***
	 * 请假记录
	 * @param ocs 机构编号集合
	 * @param rc 当前角色编号
	 * @param map
	 * @param pb
	 * @return
	 */
	public RESULT findByMap(List<Integer> ocs, Integer rc, Map<String, String> map, PageBean pb){
		
		StringBuffer temp = new StringBuffer();

		List<Object> list = new ArrayList<Object>();	
		List<Object> countParm = new ArrayList<Object>();
		temp.append(" FROM LeaveManage l");
		temp.append(" JOIN UserInfo u ON u.code = l.cu");
		temp.append(" JOIN Organ o ON o.code = l.oc");
		temp.append(" JOIN AuditStatus s ON s.mc = l.code AND s.beforeStatus = 1 WHERE l.deleted = 0 AND (s.business = 2 OR s.business = 6) AND s.rc = ? AND l.oc IN (");
		
		list.add(rc);
		countParm.add(rc);
		for (Integer i : ocs){
			temp.append("?,");
			list.add(i);
			countParm.add(i);
		}
		
		temp.deleteCharAt(temp.length() - 1).append(")");
		
		if (null != map) {
			String cuname = map.get(Parm.CUNAME);
			String username = map.get(Parm.USERNAME);
			String start = map.get(Parm.ST);
			String end = map.get(Parm.END);
			String reason = map.get(Parm.REASON);
			
			if(isNotNull(cuname)) {
				temp.append(" AND l.cu in (SELECT u.code FROM UserInfo u WHERE u.realName like ?)");
				list.add("%" + cuname + "%");
				countParm.add("%" + cuname + "%");
			}
			if(isNotNull(username)) {
				temp.append(" AND l.uc in (SELECT u.code FROM UserInfo u WHERE u.realName like ?)");
				list.add("%" + username + "%");
				countParm.add("%" + username + "%");
			}
			if(isNotNull(start)) {
				temp.append(" AND l.st > ?");
				list.add(DateUtils.getDateByStr(start, null));
				countParm.add(DateUtils.getDateByStr(start, null));
			}
			if(isNotNull(end)) {
				temp.append(" AND l.end < ?");
				list.add(DateUtils.getDateByStr(end, null));
				countParm.add(DateUtils.getDateByStr(end, null));
			}
			if(isNotNull(reason)) {
				temp.append(" AND l.reason like ?");
				list.add("%" + reason + "%");
				countParm.add("%" + reason + "%");
			} 
			
		}
		StringBuffer sql = new StringBuffer("SELECT l.code, l.cu, l.ct, l.oc, l.deleted, u.realName, o.name, l.st, l.et, l.reason");
		StringBuffer countSql = new StringBuffer("SELECT COUNT(*)");
		
		sql.append(temp.append(" ORDER BY l.code DESC"));
		countSql.append(temp);
		
		if (RESULT.SUCCESS == queryToPageBean(pb, sql.toString(), list, countSql.toString(), countParm))
			return packageResult(pb);
		
		return RESULT.ERROR;
	}
	
	
	/***
	 * 封装
	 * @param pb
	 * @return
	 */
	private RESULT packageResult(PageBean pb) {
		
		LeaveManage lm = null;
		
		List<LeaveManage> list = new ArrayList<LeaveManage>();
		
		for (Object o : pb.getResultList()) {
			
			lm = new LeaveManage();
			Object[] obj = (Object[]) o;
			
			Integer code = (null == obj[0] || Sys.NONE.equals(obj[0].toString())) ? null : (Integer) obj[0];
			Integer cu = (null == obj[1] || Sys.NONE.equals(obj[1].toString())) ? null : (Integer) obj[1];
			Date ct = (null == obj[2] || Sys.NONE.equals(obj[2].toString())) ? null : (Date) obj[2];
			Integer oc = (null == obj[3] || Sys.NONE.equals(obj[3].toString())) ? null : (Integer) obj[3];
			Boolean deleted = (null == obj[4] || Sys.NONE.equals(obj[4].toString())) ? null : (Boolean) obj[4];
			String cuname = (null == obj[5] || Sys.NONE.equals(obj[5].toString())) ? null : (String) obj[5];
			String on = (null == obj[6] || Sys.NONE.equals(obj[6].toString())) ? null : (String) obj[6];
			Date st = (null == obj[7] || Sys.NONE.equals(obj[7].toString())) ? null : (Date) obj[7];
			Date et = (null == obj[8] || Sys.NONE.equals(obj[8].toString())) ? null : (Date) obj[8];
			String reason = (null == obj[9] || Sys.NONE.equals(obj[9] .toString())) ? null :(String) obj[9];
			lm.setCode(code);
			lm.setCu(cu);
			lm.setCt(ct);
			lm.setOc(oc);
			lm.setDeleted(deleted);
			lm.setCuname(cuname);
			lm.setOn(on);
			lm.setSt(st);
			lm.setEt(et);
			lm.setReason(reason);
			list.add(lm);
		}
		
		pb.setResultList(list);
		
		return RESULT.SUCCESS;
	}
	
	/***
	 * 
	 * @param mc
	 * @param rc
	 * @return
	 */
	public List<AuditStatus> findAuditStatusBusiness(Integer mc, Integer rc, Byte business) {
		if (null == mc || null == rc)return null;
		return find(sql_findAuditStatusBusiness, null, null, mc, rc, business);
	}
	//查询所有离职信息
		public RESULT findByMap_2(List<Integer> ocs,Map<String, String> map,PageBean pb){
			StringBuffer hql = new StringBuffer("FROM LeaveManage r WHERE r.pass = 3 AND r.oc IN (");
			List<Object> list = new ArrayList<Object>();
			for (Integer i : ocs) {
				hql.append("?,");
				list.add(i);
			}
			hql.deleteCharAt(hql.length() - 1).append(")");
			if(null != map) {
				String start = map.get(Parm.ST);
				String end = map.get(Parm.END);
				String un = map.get(Parm.UN);
				String oc = map.get(Parm.OC);
				String reason = map.get(Parm.REASON);
				String deleted = map.get(Parm.DELETED);
				if(isNotNull(start)) {
					hql.append(" AND r.ct < ?");
					list.add(DateUtils.getDateByStr(start, "yyyy-MM-dd"));
				}if(isNotNull(end)) {
					hql.append(" AND r.ct > ?");
					list.add(DateUtils.getDateByStr(end, "yyyy-MM-dd"));
				}if(isNotNull(un)) {
					hql.append(" AND r.uc in (SELECT u.code FROM UserInfo u WHERE u.realName LIKE ?)");
					list.add("%" + un + "%");
				}if(isNotNull(oc)) {
					hql.append(" AND r.oc = ?");
					list.add(Integer.parseInt(oc));
				}if(isNotNull(reason)) {
					hql.append(" AND r.reason LIKE ?");
					list.add("%" + reason + "%");
				}if(isNotNull(deleted)) {
					hql.append(" AND r.deleted = ?");
					list.add(Boolean.parseBoolean(deleted));
				}
			}
			hql.append(" ORDER BY r.code DESC");
			
			if (findToPageBean(hql.toString(), pb, list) == RESULT.SUCCESS)
				return RESULT.SUCCESS;
			
			return RESULT.ERROR;
		}
}
